/***************************************************************************
 //* Foxit Software Inc.
 //* Copyright(C) 2005-2012, all rights reserved.
 //*
 //* The following code is copyrighted and contains proprietary information
 //* and trade secrets of Foxit Software Inc.                                                      *
 ***************************************************************************/

/**
 * Environment for browser
 */
define(function (require, exports, module) {  
    var WebPDF = require('./WebPDF.js');
    var envWrapper = (function () {
        var agent = navigator.userAgent.toLowerCase();
        var opera = window.opera;

        var env =
        {
            /**
             IE
             */
            ie: /msie/.test(agent) && !/opera/.test(agent) && document.all,
            /**
             * IE11 at least.
             */
            ieAtLeast11: !/msie/.test(agent) && /trident/.test(agent) && !/opera/.test(agent),
            /**
             Opera
             */
            opera: (!!opera && opera.version),

            /**
             *   WebKit based browser, such as Safari.
             */
            webkit: (agent.indexOf(' applewebkit/') > -1),
            chrome: agent.indexOf('chrome') > -1,
            safari: agent.indexOf('safari') > -1 && agent.indexOf('chrome') == -1,
            firefox: agent.indexOf('firefox') > -1,
            /**
             * Macintosh.
             */
            mac: (agent.indexOf('macintosh') > -1),

            /**
             *  quirks mode
             */
            quirks: (document.CompactMode == 'BackCompact'),

            /**
             *  iPhone/iPad/iPod
             */
            iOS: /(ipad|iphone|ipod)/.test(agent),
            android : /android/i.test(agent),
            blackberry : /blackberry/i.test(agent),
            webos : /webos/i.test(agent),
            kindle : /silk|kindle/i.test(agent),
            /**
             https
             */
            secure: location.protocol == 'https:'
        };

        env.mobile = /mobile/i.test(agent) || env.iOS || env.android || env.blackberry || env.webos || env.kindle;
        if(env.mobile){
            var isElasticScrollSupported = true;
            if (env.iOS) {
                var iOSVersion = (navigator.appVersion).match(/OS (\d+)_(\d+)_?(\d+)?/);
                if(iOSVersion && iOSVersion.length>0 && parseInt(iOSVersion[1]) < 4){
                    console.log("Version of IOS is lower than 4.0");
                    isElasticScrollSupported = false;
                }
            }
            else if(env.android){
                var matchs = agent.match(/android\s+([\d\.]+)/);
                if(matchs && matchs.length > 0){
                    var androidVersion = agent.match(/android\s+([\d\.]+)/)[1];
                    if(parseFloat(androidVersion) < 4){
                        console.log("Version of android is lower than 4.0");
                        isElasticScrollSupported = false;
                    }
                }
                else{
                    isElasticScrollSupported = false;
                }


            }
            $.support.isElasticScrollSupported = isElasticScrollSupported;
        }

        /**
         * Gecko based browser, such as Firefox.
         */
        env.gecko = (navigator.product == 'Gecko' && !env.webkit && !env.opera);

        var version = 0;

        // Internet Explorer 6.0+
        if (env.ie) {
            version = parseFloat(agent.match(/msie (\d+)/)[1]);

            /**
             ie8
             */
            env.ie8 = !!document.documentMode && document.documentMode < 8;

            /**
             ie8 standard mode.
             */
            env.ie8Compact = document.documentMode == 8;

            /**
             ie9 standard mode.
             */
            env.ie9Compact = document.documentMode == 9;

            /**
             ie7 standard mode.
             */
            env.ie7Compact = ((version == 7 && !document.documentMode)
                || document.documentMode == 7);

            /**
             ie6 standard mode.
             */
            env.ie6Compact = (version < 7 || env.quirks);
            env.ie8OrLower = env.ie8 || env.ie8Compact || env.ie7Compact  || env.ie6Compact;
        }
        else
            env.ie8OrLower = false;

        // Gecko.
        if (env.gecko) {
            var geckoRelease = agent.match(/rv:([\d\.]+)/);
            if (geckoRelease) {
                geckoRelease = geckoRelease[1].split('.');
                version = geckoRelease[0] * 10000 + (geckoRelease[1] || 0) * 100 + (geckoRelease[2] || 0) * 1;
            }
        }

        // Opera 9.50+
        if (env.opera)
            version = parseFloat(opera.version());

        // WebKit 522+ (Safari 3+)
        if (env.webkit)
            version = parseFloat(agent.match(/ applewebkit\/(\d+)/)[1]);

        env.version = version;

        /**
         check whether current browser is compatible.
         */
        env.isCompatible =
            env.iOS && version >= 534 ||

            !env.mobile && (

            (env.ie && version >= 6) ||
            (env.gecko && version >= 10801) ||
            (env.opera && version >= 9.5) ||
            (env.webkit && version >= 522) ||
            false);

        return env;
    })();

    /**
     * @namespace
     * @memberOf WebPDF
     * @description Environment of the browser.
     */
    WebPDF.Environment =
    {
        /**
         IE
         */
        ie: envWrapper.ie,
        chrome: envWrapper.chrome,
        safari: envWrapper.safari,
        /**
         * IE11 at least.
         */
        ieAtLeast11: envWrapper.ieAtLeast11,
        /**
         Opera
         */
        opera: envWrapper.opera,

        /**
         *   WebKit based browser, such as Safari.
         */
        webkit: envWrapper.webkit,

        /**
         * Macintosh.
         */
        mac: envWrapper.mac,

        /**
         *  quirks mode
         */
        quirks: envWrapper.quirks,

        /**
         *  mobile like
         */
        mobile: envWrapper.mobile,

        /**
         *  iPhone/iPad/iPod
         */
        iOS: envWrapper.iOS,


        /**
         https
         */
        secure: envWrapper.secure,
        version: envWrapper.version,
        /**
         ie8
         */
        ie8: envWrapper.ie8,

        /**
         ie8 standard mode.
         */
        ie8Compact: envWrapper.ie8Compact,

        /**
         ie9 standard mode.
         */
        ie9Compact: envWrapper.ie9Compact,

        /**
         ie7 standard mode.
         */
        ie7Compact: envWrapper.ie7Compact,

        /**
         ie6 standard mode.
         */
        ie6Compact: envWrapper.ie6Compact,
        ie8OrLower: envWrapper.ie8OrLower,
        isCompatible: envWrapper.isCompatible,
        firefox: envWrapper.firefox
    };

    return WebPDF.Environment;
});